package com.supermap.dataservice.cloud.cust.menu;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.cloud.cust.menu.SysMenu;
import com.supermap.dubbo.api.cloud.cust.menu.SysMenuService;
import com.supermap.dubbo.api.cloud.cust.menu.TreeNode;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author aqua
 * @desc
 * @date 2019年02月21 11:06
 * @email 1301584033@qq.com
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Resource
    private SysMenuMapper sysMenuMapper;



    @Override
    public JSONUtil getSysMenuList(Integer userId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        List<SysMenu> sysMenus= sysMenuMapper.getSysMenuList(userId,3,null);
        for(SysMenu sysMenu:sysMenus){
            sysMenu.setSysMenus(getMenuList(sysMenu,userId));
        }
        return new JSONUtil().setData(sysMenus);
    }

    public List<SysMenu> getMenuList(SysMenu sysMenu,Integer userId){
        List<SysMenu> sysMenus=new ArrayList<>();
        if(sysMenu.getCategory()!=1){
            return  sysMenus;
        }
        List<SysMenu>  sysMenuList=sysMenuMapper.getSysMenuList(userId,sysMenu.getCode().length()+3,sysMenu.getCode());
        for(SysMenu sysMenu1:sysMenuList) {
            if (sysMenu1 != null) {
                sysMenu1.setSysMenus(getMenuList(sysMenu1,userId));
                sysMenus.add(sysMenu1);
            } else {
                continue;
            }
        }
        return sysMenus;
    }
    @Override
    public JSONUtil getCostomSysMenuList(String areaCode,Integer userId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        List<SysMenu> sysMenus = sysMenuMapper.getAreaSysMenuList(areaCode, 3, null, userId);
        for (SysMenu sysMenu : sysMenus) {
            List<SysMenu> menuList= getAreaMenuList(sysMenu, areaCode, userId);
            sysMenu.setSysMenus(menuList);
        }
        return new JSONUtil().setData(sysMenus);
    }

    public List<SysMenu> getAreaMenuList(SysMenu sysMenu,String areaCode,Integer userId){
        List<SysMenu> sysMenus = new ArrayList<>();
        if(sysMenu.getCategory()!=1){
            return  sysMenus;
        }
        List<SysMenu>  sysMenuList=sysMenuMapper.getAreaSysMenuList(areaCode,sysMenu.getCode().length()+3,sysMenu.getCode(),userId);
        for(SysMenu sysMenu1:sysMenuList) {
            if (sysMenu1 != null) {
                List<SysMenu>  sysMenus1=  getAreaMenuList(sysMenu1,areaCode,userId);
                sysMenu1.setSysMenus(sysMenus1);
                sysMenus.add(sysMenu1);
            } else {
                continue;
            }
        }
        return sysMenus;
    }


    @Override
    public JSONUtil putCostomMenu(Integer menuid, Integer userId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        Integer result=  sysMenuMapper.putIsExist(userId,menuid);
          if(result==null){
                return   new JSONUtil().setCode(100).setMsg("该功能属于特有功能请升级相关角色即可查看");
          }

        //添加目录
        //通过menuid取Code
        foundMenu(menuid,userId);
        Integer category= sysMenuMapper.iscategory(menuid);
          if(category==1){
             List<Integer> integers=  sysMenuMapper.getCodes(menuid);
             for(Integer a:integers){
                 sysMenuMapper.putSysMenu(a,userId);
                 sysMenuMapper.putSysUserOrderMenu(a,userId);
             }
          }else{
              sysMenuMapper.putSysMenu(menuid,userId);
              sysMenuMapper.putSysUserOrderMenu(menuid,userId);
          }

        return new JSONUtil();
    }

    //生产目录
    public void foundMenu(Integer menuid, Integer userId){
        String code= sysMenuMapper.getCodeByMenuId(menuid);
        if(code.length()>3){
            Integer count=sysMenuMapper.getExitMenu(userId,code.substring(0,code.length()-3));
            if(count>0){
                //存在不需要创建目录
            }else{
               Integer newMenuid=sysMenuMapper.getMenuIdByCode(code.substring(0,code.length()-3));
                sysMenuMapper.putSysMenu(newMenuid,userId);
                foundMenu(newMenuid,userId);
            }
        }

    }
    @Override
    public JSONUtil delCostomMenu(Integer menuid, Integer userId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");

        Integer category= sysMenuMapper.iscategory(menuid);
        if(category==1){
            List<Integer> integers=  sysMenuMapper.getCodes(menuid);
            for(Integer a:integers){
                sysMenuMapper.delCostomMenu(a,userId);
                sysMenuMapper.delUserMenu(a,userId);
            }
        }else{
            sysMenuMapper.delCostomMenu(menuid,userId);
            sysMenuMapper.delUserMenu(menuid,userId);
        }

        return  new JSONUtil();
    }

    @Override
    public String getSysMenuCode(String code) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        String orderCode="";
        String newCode=null;
        if(code.length()==1){
            orderCode= sysMenuMapper.getCode(3,"A");
        }else {
            orderCode = sysMenuMapper.getCode(code.length() + 3,code);
        }
        if(orderCode==null){
            newCode= code+"001";
            return newCode;
        }
        if(orderCode.length()==3){
            newCode=orderCode.substring(0,1)+getOrderCode(orderCode.substring(orderCode.length()-2,orderCode.length()));
        }else{
            newCode=orderCode.substring(0,orderCode.length()-3)+getOrderCode(orderCode.substring(orderCode.length()-3,orderCode.length()));
        }
        return newCode;
    }

    public String getOrderCode(String orderCode){
        String newOrderCode=null;
        if(orderCode.length()==2){
              newOrderCode=String.valueOf(Integer.valueOf(orderCode)+1);
              if(newOrderCode.length()==1){
                  newOrderCode="0"+newOrderCode;
              }

        }else{
            newOrderCode=String.valueOf(Integer.valueOf(orderCode)+1);
            if(newOrderCode.length()==1){
                newOrderCode="00"+newOrderCode;
            } else if(newOrderCode.length()==2){
                newOrderCode="0"+newOrderCode;
            }
        }
        return  newOrderCode;
    }
    @Override
    public JSONUtil getSysMenuById(Integer menuId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtil().setData(sysMenuMapper.getSysMenuById(menuId));
    }

    @Override
    public JSONUtil getSysMenuTreeNodeList(String areaCode,Integer roleId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        //第一层
         List<TreeNode> treeNodes=sysMenuMapper.getSysMenuTreeNodeList(areaCode,roleId,3,null);
         if(treeNodes.size()>0&&treeNodes!=null){
             for(int i=0;i<treeNodes.size();i++){
                     treeNodes.addAll( getChildNode(treeNodes.get(i),areaCode,roleId));
             }
         }
        TreeNode treeNode=new TreeNode();
        treeNode.setId(0);
        treeNode.setCode("0");
        treeNode.setName("全部节点");
        treeNode.setOpen(true);
        treeNodes.add(treeNode);
        return new JSONUtil().setData(treeNodes);
    }
    //生产树节点
    public List<TreeNode> getChildNode(TreeNode treeNode,String areaCode,Integer roleId){
        List<TreeNode> newTreeNodes=new ArrayList<>();
           if(treeNode.getCategory()!=1){
                return  newTreeNodes;
           }

            List<TreeNode>  treeNodes1= sysMenuMapper.getSysMenuTreeNodeList(areaCode,roleId,treeNode.getCode().length()+3,treeNode.getCode());
            for(TreeNode treeNode1:treeNodes1) {
                if (treeNode1 != null) {
                    newTreeNodes.add(treeNode1.setpId(treeNode.getId()));
                    getChildNode(treeNode1, areaCode, roleId);

                } else {
                    continue;
                }
            }
        return  newTreeNodes;
    }

    @Override
    public JSONUtil insertSysMenu(SysMenu sysMenu) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        sysMenuMapper.insertSysMenu(sysMenu);
        return new JSONUtil();
    }

    @Override
    public JSONUtil updateSysMenu(SysMenu sysMenu) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        sysMenuMapper.updateSysMenu(sysMenu);
        return new JSONUtil();
    }

    @Override
    public JSONUtil getSysMenuArea(Integer menuId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        TreeNode treeNode=new TreeNode();
        treeNode.setId(0);
        treeNode.setCode("0");
        treeNode.setName("全部节点");
        treeNode.setOpen(true);
        List<TreeNode> treeNodes=sysMenuMapper.getAllArea(menuId);
        for(TreeNode treeNode1:treeNodes){
                if(treeNode1.getCategory()==1){
                    treeNode1.setChecked(true);
                }
        }
        treeNodes.add(treeNode);
        return  new JSONUtil().setData(treeNodes);
    }

    @Override
    public JSONUtil putSysMenuArea(Integer menuId, String areaCode) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        String  areaCodes[]=areaCode.split(",");
        //删除菜单
        sysMenuMapper.delSysAreaMenu(menuId);
        for(String str:areaCodes){
            putSysMenuAreafound(menuId,str);
            sysMenuMapper.putSysMenuArea(menuId,str);
        }

        return new JSONUtil();
    }

    //生产目录
    public void putSysMenuAreafound(Integer menuid, String areaCode){
        String code= sysMenuMapper.getCodeByMenuId(menuid);
        if(code.length()>3){
            Integer count=sysMenuMapper.getExitAreaMenu(areaCode,code.substring(0,code.length()-3));
            if(count>0){
                //存在不需要创建目录
            }else{
                Integer newMenuid=sysMenuMapper.getMenuIdByCode(code.substring(0,code.length()-3));
                sysMenuMapper.putSysMenuArea(newMenuid,areaCode);
                putSysMenuAreafound(newMenuid,areaCode);
            }
        }

    }



}
